package com.spring.bean.beanFactoryPostprocessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionVisitor;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.util.StringValueResolver;

import java.util.HashSet;
import java.util.Set;

public class BeanFactoryPostprocessorCore implements BeanFactoryPostProcessor {
    private Set<String> obscenties;
    public BeanFactoryPostprocessorCore() {
        obscenties = new HashSet<String>();
    }

    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        String[] arr = beanFactory.getBeanDefinitionNames();
        for (String str : arr) {
            BeanDefinition bf = beanFactory.getBeanDefinition(str);
            StringValueResolver valueResolver = new StringValueResolver() {
                public String resolveStringValue(String strVal) {
                    if (isObscene(strVal)) {
                        return "**************";
                    }
                    return strVal;
                }
            };
            BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
            visitor.visitBeanDefinition(bf);
        }
    }
    private Boolean isObscene(Object value) {
        String postntialObscenity = value.toString().toUpperCase();
        return this.obscenties.contains(postntialObscenity);
    }

    public void setObscenties(Set<String> obscenties) {
        this.obscenties.clear();
        for (String obscenity:obscenties) {
            this.obscenties.add(obscenity);
        }
    }
}
